package facade;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import model.BooleanModel;
import model.Delivery;
import model.Restaurant;

import controler.AddListener;
import controler.ConfirmCommandListener;
import controler.RemoveListener;
import controler.ResetListener;
import controler.SetDeliveryInfoListener;
import controler.SetMenuListener;
import controler.StartListener;
import controler.StopListener;
import controler.ChangeRestaurantListener;

import template.AbstractSystem;

import utils.User;
import utils.Date;
import utils.Time;

import view.CustomMenuItem;
import view.FacturePreview;
import view.MenuSelectionSection;
import view.DeliveryFormContainer;
import view.RestaurantChooser;
import view.CustomButton;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Container;

import java.util.Observable;

public class ClientSystem extends AbstractSystem{
	
	private static final long serialVersionUID = 1L;
	
	private Delivery delivery;
	private BooleanModel boolModelMenu;
	private BooleanModel boolModelRestaurantChooser;
	private BooleanModel boolModelButton;
	
	private JPanel menuSelectionSection;
	private JList facturePreviewList;
	
	public ClientSystem(){
		
		delivery = new Delivery();
		boolModelMenu = new BooleanModel();
		boolModelRestaurantChooser = new BooleanModel();
		boolModelButton = new BooleanModel();
		
		setCurrentSystem(this);
		addWindowListener(new CloseSystemListener());
		
		setTitle("Client System");
		setLayout(null);
		setBounds((int)dim.getWidth()/2-450, (int)dim.getHeight()/2-300, 900, 600);
		setBackground(Color.WHITE);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setJMenuBar(createMainMenu());
        setContentPane(createDeliveryFormContainer());
        setVisible(true);
	}
	
	private JMenuBar createMainMenu(){
		JMenuBar mainMenu = createRootMainMenu("Client System");	
		
		JMenu menuCommand = new JMenu("Command");
		
		JMenuItem startCommandMenuItem = new CustomMenuItem(boolModelMenu, "Start command");
		startCommandMenuItem.setEnabled(false);
		startCommandMenuItem.addActionListener(new StartListener(boolModelMenu, boolModelRestaurantChooser));
		menuCommand.add(startCommandMenuItem);
		
		JMenuItem resetItem = new CustomMenuItem(boolModelMenu, "Reset command");
		resetItem.addActionListener(new ResetListener(delivery, boolModelRestaurantChooser, boolModelButton));
		menuCommand.add(resetItem);
		
		JMenuItem stopItem = new CustomMenuItem(boolModelMenu, "Stop command");
		stopItem.addActionListener(new StopListener(delivery, boolModelMenu, boolModelButton));
		menuCommand.add(stopItem);
		
		mainMenu.add(menuCommand);
		
		return mainMenu;
	}
	
	private JPanel createDeliveryFormContainer(){
		JPanel deliveryFormContainer = new DeliveryFormContainer(boolModelMenu);
		deliveryFormContainer.setLayout(null);
		deliveryFormContainer.setBackground(Color.WHITE);	
		
		deliveryFormContainer.add(createFacturePreviewList());
    	deliveryFormContainer.add(createFactureContainer());
    	deliveryFormContainer.add(createRestaurantsTab());
        
    	return deliveryFormContainer;
	}
	
    private JTabbedPane createRestaurantsTab(){
    	
    	RestaurantChooser restaurantsTab = new RestaurantChooser(boolModelRestaurantChooser);    	
        restaurantsTab.addChangeListener(new ChangeRestaurantListener(delivery));
        
        restaurantsTab.addTab("Momo pizza", createDeliveryForm(new Restaurant("Momo pizza")));
    	restaurantsTab.addTab("Boston pizza", createDeliveryForm(new Restaurant("Boston pizza")));
    	restaurantsTab.addTab("Subway", createDeliveryForm(new Restaurant("Subway")));
    	restaurantsTab.addTab("MacDonald", createDeliveryForm(new Restaurant("MacDonald")));
    	
        float selectedIndex = (float)Math.random()*(restaurantsTab.getTabCount()-1);
        restaurantsTab.setSelectedIndex(Math.round(selectedIndex));
        delivery.setRestaurantName(restaurantsTab.getTitleAt(Math.round(selectedIndex)));
        
        
        return restaurantsTab;
    }
    
    private JPanel createDeliveryForm(Observable restaurant){
    	JPanel restaurantForm = new JPanel();
    	restaurantForm.setLayout(null);
    	
    	restaurantForm.setPreferredSize(new Dimension(750, 600));
    	createDateTimeInfosSection(restaurantForm);
    	createAdressInfosSection(restaurantForm);
    	restaurantForm.add(createMenuSectionsSection(restaurant));
    	restaurantForm.add(createMenuSelectionSectionContainer(restaurant)); 
    	restaurantForm.add(createAddButton());
    	restaurantForm.add(createRemoveButton());
    	
    	return restaurantForm;
    }
    
    private JButton createAddButton(){
    	
    	JButton addButton = new JButton("Add");
    	addButton.setBounds(330, 415, 200, 20);
    	addButton.addActionListener(new AddListener(delivery, boolModelRestaurantChooser, boolModelButton, menuSelectionSection));
    	return addButton;
    }

    private JButton createRemoveButton(){
    	
    	JButton removeButton = new CustomButton(boolModelButton, "Remove");
    	removeButton.setBounds(330, 440, 200, 20);
    	removeButton.addActionListener(new RemoveListener(delivery,boolModelRestaurantChooser, boolModelButton, facturePreviewList));
    	return removeButton;
    }
    
    private JPanel createFactureContainer(){
    	JPanel factureContainer = new JPanel();
    	factureContainer.setBounds(598,21,295,503);
		factureContainer.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
		factureContainer.add(createButtonConfirmCommand());
		factureContainer.add(createLabelFactureSommary());
    	return factureContainer;
    }
    
    private JList createFacturePreviewList(){
    	facturePreviewList = new FacturePreview(delivery);
    	facturePreviewList.setBounds(600, 100, 291, 400);
    	return facturePreviewList;
    }
    
    private void createDateTimeInfosSection(Container c){
    	
    	String[] timeInfos = {"Date", "Time"};
    	String[] currentTimeInfos = {Date.getDate(), Time.getTime()};
    	
    	for(int i = 0; i < timeInfos.length; i++){
    		
    		JLabel timeInfoLabel = new JLabel(timeInfos[i]+" : "); 
    		JTextField infoTextField = new JTextField(currentTimeInfos[i]);
    		
    		timeInfoLabel.setBounds(325, 340 + i*25, 50, 20);
    		timeInfoLabel.setFont(new Font("Arial", Font.BOLD, 15));
    		infoTextField.setBounds(375, 340 + i*25, 150, 20);
    		infoTextField.addFocusListener(new SetDeliveryInfoListener(delivery));
    		infoTextField.setName(timeInfos[i]);
    		
    		c.add(timeInfoLabel);
    		c.add(infoTextField);
    		c.repaint();
    	}
    	
    }
    
    private void createAdressInfosSection(Container c){
    	
        JLabel deliveryAdress = new JLabel ("Delivery Address");
        deliveryAdress.setBounds (5,300,200,50);
        deliveryAdress.setFont(new Font ("Arial", Font.BOLD, 20)); 
        
        String[] adressInfos = {"Civic address", "City", "Postal code", "Province", "Country"};
        String[] clientAdressInfos = {User.CIVICADDRESS, User.CITY, User.ZIPCODE, User.PROVINCE, User.COUNTRY};
        
        for(int i = 0; i < 5; i++){
        	
        	JLabel adressInfosLabel = new JLabel(adressInfos[i]+" : ");
        	adressInfosLabel.setBounds(15, 340 + i*25, 150, 20);
        	adressInfosLabel.setFont(new Font("Arial", Font.BOLD, 15));
        	
        	JTextField adressInfosTextField = new JTextField(clientAdressInfos[i]);
        	adressInfosTextField.setName(adressInfos[i]);
        	adressInfosTextField.addFocusListener(new SetDeliveryInfoListener(delivery));
        	adressInfosTextField.setBounds(150, 340 + i*25, 150, 20);
        	
        	c.add(adressInfosLabel);
        	c.add(adressInfosTextField);
        }
        
        c.add(deliveryAdress);
        c.repaint();
    }
    
    private JList createMenuSectionsSection(Observable restaurant){
    	
    	String[] list = {"Appetizers", "Soups", "Chickens", "Ribs", "Sandwiches", "Salads", "Kids", "Desserts", "Beverages", "Extras"};
    	JList listDishes = new JList(list);
    	
    	listDishes.setBounds(5,5,150,300);
    	listDishes.setSelectedIndex(0);
        listDishes.setBorder(BorderFactory.createEtchedBorder(Color.DARK_GRAY, Color.LIGHT_GRAY));
        
        listDishes.addListSelectionListener(new SetMenuListener(restaurant));
        
        return listDishes;
    }
    
    private JPanel createMenuSelectionSectionContainer(Observable restaurant){
    	
    	menuSelectionSection = new MenuSelectionSection(restaurant);
        Restaurant model = (Restaurant)restaurant;
        model.setMenuSection("Appetizers");
    	
        return menuSelectionSection;
    }
    
    private JButton createButtonConfirmCommand(){
    	
        JButton confirmButton = new CustomButton(boolModelButton, "Confirm your command");
        confirmButton.setBounds(610, 6, 100, 30);
        confirmButton.addActionListener(new ConfirmCommandListener(delivery, boolModelMenu));
        return confirmButton;
    }
    
    private JLabel createLabelFactureSommary(){
    	
        JLabel titreFacture = new JLabel("Summary of the bill");
        titreFacture.setBounds(610, 0, 100, 30);
        titreFacture.setFont(new Font("Arial", Font.BOLD, 20));
        return titreFacture;
    }
}
